package com.zis.wechat.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zis.starter.core.Constant;
import com.zis.starter.core.ResponseEntity;
import com.zis.starter.core.SearchParams;
import com.zis.starter.redis.RedisUtil;
import com.zis.wechat.api.config.WxConstant;
import com.zis.wechat.dao.TAccountDao;
import com.zis.wechat.model.TAccount;
import com.zis.wechat.service.ITAccountInter;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信公众号配置
 * Create by wangshen 2018/7/18
 */
@Service
@Transactional("txManager")
public class TAccountInterImpl implements ITAccountInter {

    @Autowired
    private TAccountDao accountDao;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 新增
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity insert(TAccount account) throws Exception {
        int num = accountDao.getSQLManager().insert(account);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    /**
     * 修改
     * @param account
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity update(TAccount account) throws Exception {
        int num = accountDao.getSQLManager().updateTemplateById(account);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    /**
     * 删除
     * @param accountId
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity delete(String accountId) throws Exception {
        int num = accountDao.getSQLManager().deleteById(TAccount.class, accountId);
        if (num > 0) {
            return ResponseEntity.success(Constant.DATA_RESULT_SUCCEED);
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_FAIL);
    }

    /**
     * 根据ID查询
     * @param accountId
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity selectById(String accountId) throws Exception {
        TAccount account = accountDao.getSQLManager().unique(TAccount.class, accountId);
        if (account != null) {
            return ResponseEntity.success(JSON.parseObject(JSON.toJSONString(account)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    /**
     * 分页查询
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public PageQuery<TAccount> findByPage(SearchParams params) throws Exception {
        PageQuery<TAccount> query = new PageQuery<>();
        query.setPageNumber(params.getPage());
        query.setPageSize(params.getPageSize());
        query.setParas(params.getParams());
        accountDao.getSQLManager().pageQuery("wechat.wechatAccount.query", TAccount.class, query);
        return query;
    }

    /**
     * 条件查询
     * @param params
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity findByCondition(Map params) throws Exception {
        List resultList = accountDao.getSQLManager().select("wechat.wechatAccount.query", TAccount.class, params);
        if (resultList != null && resultList.size() > 0) {
            return ResponseEntity.success(JSON.parseArray(JSON.toJSONString(resultList)));
        }
        return ResponseEntity.failure(Constant.DATA_RESULT_NULL);
    }

    /**
     * 设置缓存信息
     * @return
     * @throws Exception
     */
    @Override
    public ResponseEntity setCacheConfig() throws Exception {
        Map<String, Object> cacheMap = new HashMap<>();
        List<TAccount> accountList = accountDao.getSQLManager().all(TAccount.class);
        for (TAccount account : accountList) {
            JSONObject jsonObj = new JSONObject();
            jsonObj.put("accountId", account.getAccountId());
            jsonObj.put("accountName", account.getAccountName());
            jsonObj.put("appId", account.getAccountId());
            jsonObj.put("appSecret", account.getAppsecret());
            jsonObj.put("originalId", account.getOriginalId());
            jsonObj.put("token", account.getToken());
            jsonObj.put("aesKey", account.getAeskey());
            jsonObj.put("servUrl", account.getServUrl());
            jsonObj.put("pageTemplate", account.getPageTemplate());
            cacheMap.put(account.getAccountId(), jsonObj);
            redisUtil.setHash(WxConstant.WECHAT_ACCESS_TOKEN_KEY, account.getAccountId(), jsonObj);
        }
        return ResponseEntity.success("缓存信息刷新成功");
    }
}
